package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        //插入套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        //插入套餐关联的菜品数据
        Long id = setmeal.getId();//获取套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断套餐中是否存在菜品
        if (setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
        }
        //批量插入套餐中的菜品
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置PageHelp的基本参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //传递查询条件
        Integer categoryId = setmealPageQueryDTO.getCategoryId();
        String name = setmealPageQueryDTO.getName();
        Integer status = setmealPageQueryDTO.getStatus();

        Page<SetmealVO> setmealList = setmealMapper.pageQuery(categoryId,name,status);
        return new PageResult(setmealList.getTotal(),setmealList.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //从套餐表中查询套餐相关数据
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        //查询分类名称
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryMapper.selectById(categoryId);
        String categoryName = category.getName();
        setmealVO.setCategoryName(categoryName);

        //查询套餐包含的菜品
        List<SetmealDish> dishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(dishes);
        //从套餐菜品表中查询套餐中的菜品
        return setmealVO;
    }

    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);//修改套餐的信息

        //修改套餐相关的菜品信息
        setmealDishMapper.deleteBysetmealId(setmealDTO.getId());
        //重新插入套餐中的菜品信息
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if (dishes != null && dishes.size() > 0){
            for (SetmealDish dish : dishes) {
                dish.setSetmealId(setmealDTO.getId());
            }
        }
        setmealDishMapper.insertBatch(dishes);
    }

    /**
     * 修改套餐的状态
     * @param status
     * @param id
     */
    public void stopOrStart(Integer status, Long id) {
        //如果套餐中存在未启售得菜品，套餐无法启售
        if (status == StatusConstant.ENABLE){
            List<SetmealDish> dishes = setmealDishMapper.selectBySetmealId(id);
            for (SetmealDish setmealDish : dishes) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                if (dish.getStatus() == StatusConstant.DISABLE)
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }

        }
        //修改套餐的状态
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //套餐启售中不能删除
        //遍历套餐id，确认是否存在启售中得套餐
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            //启售中得菜品不能删除
            if (setmeal.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除套餐信息和套餐关联的菜品信息
        for (Long id : ids) {
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBysetmealId(id);
        }
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
